1 package net.sourceforge.simplegamenet.connectaline;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import javax.swing.*;
6 import net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel;
7 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9
10 public class CALSettingsPanel extends GameSettingsPanel
11 implements ActionListener, CALPlayStyle {
12
13 private static final String[][] valueList = {
14 {
15 "2-player game with gravity (classic)",
16 "2", "8", "4", "7", "6", "gravity"
17 },
18 {
19 "2-player game without gravity", "2",
20 "8", "5", "9", "9", "no gravity"
21 },
22 {
23 "3-player game with gravity ", "3", "8",
24 "3", "6", "6", "gravity"
25 },
26 {
27 "3-player game without gravity", "3",
28 "8", "4", "9", "9", "no gravity"
29 },
30 {
31 "4-player game without gravity", "4",
32 "8", "4", "10", "10", "no gravity"
33 }
34 };
35
36 private JLabel predefinedSettingsLabel = new JLabel("Predefined Settings:");
37 private JLabel[] settingLabels = {
38 new JLabel("Number of players:"),
39 new JLabel("Maximum number of connections:"),
40 new JLabel("Length of a line:"),
41 new JLabel("Number of columns:"),
42 new JLabel("Number of rows:"),
43 new JLabel("Play style:")
44 };
45
46
47
48
49
50
51
52 private JComboBox[] settingComboBoxes = new JComboBox[valueList[0].length];
53
54
55
56
57
58
59
60
61 private JPanel subPanel;
62
63 public CALSettingsPanel(int maxNumberOfPlayer, int maxNumberOfcon, int lengthOfALine,
64 int numberOfColumns, int numberOfRows, int playStyle)
65 throws IllegalArgumentException {
66 ProportionLayout layout = new ProportionLayout();
67 layout.appendColumn(0, ProportionLayout.NO_PROPORTION);
68 layout.appendColumn(10);
69 layout.appendColumn(0, 1.0);
70 layout.appendRow(0, 1.0);
71 layout.appendRow(0, ProportionLayout.NO_PROPORTION);
72 layout.appendRow(10);
73 layout.appendRow(0, ProportionLayout.NO_PROPORTION);
74 layout.appendRow(0, 1.0);
75 setLayout(layout);
76
77 add(predefinedSettingsLabel, new ProportionConstraints(0, 1));
78 settingComboBoxes[0] = new JComboBox();
79 for (int i = 0; i < valueList.length; i++) {
80 settingComboBoxes[0].addItem(valueList[i][0]);
81 }
82 settingComboBoxes[0].addItem("Custom");
83 add(settingComboBoxes[0], new ProportionConstraints(2, 1));
84
85 ProportionLayout subLayout = new ProportionLayout();
86 subLayout.appendColumn(5);
87 subLayout.appendColumn(0, ProportionLayout.NO_PROPORTION);
88 subLayout.appendColumn(10);
89 subLayout.appendColumn(0, 1.0);
90 subLayout.appendColumn(5);
91 subLayout.appendRow(5);
92 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
93 subLayout.appendRow(10);
94 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
95 subLayout.appendRow(10);
96 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
97 subLayout.appendRow(10);
98 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
99 subLayout.appendRow(10);
100 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
101 subLayout.appendRow(10);
102 subLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
103 subLayout.appendRow(5);
104 subPanel = new JPanel(subLayout);
105 subPanel.setBorder(BorderFactory.createTitledBorder(""));
106 for (int i = 1; i < settingComboBoxes.length; i++) {
107 settingComboBoxes[i] = new JComboBox();
108 }
109 for (int i = 2; i <= 6; i++) {
110 settingComboBoxes[1].addItem(Integer.toString(i));
111 }
112 for (int i = 2; i <= 16; i++) {
113 settingComboBoxes[2].addItem(Integer.toString(i));
114 }
115 for (int i = 3; i <= 6; i++) {
116 settingComboBoxes[3].addItem(Integer.toString(i));
117 }
118 for (int i = 5; i <= 15; i++) {
119 settingComboBoxes[4].addItem(Integer.toString(i));
120 settingComboBoxes[5].addItem(Integer.toString(i));
121 }
122 settingComboBoxes[6].addItem("gravity");
123 settingComboBoxes[6].addItem("no gravity");
124 for (int i = 0; i < settingComboBoxes.length; i++) {
125 settingComboBoxes[i].setSelectedItem(valueList[0][i]);
126 settingComboBoxes[i].addActionListener(this);
127 }
128 for (int i = 0; i < settingLabels.length; i++) {
129 subPanel.add(settingLabels[i], new ProportionConstraints(1, 1 +
130 (i * 2)));
131 }
132 for (int i = 1; i < settingComboBoxes.length; i++) {
133 subPanel.add(settingComboBoxes[i], new ProportionConstraints(3,
134 (i * 2) - 1));
135 }
136 add(subPanel, new ProportionConstraints(0, 3, 3, 1));
137
138 settingComboBoxes[1].setSelectedItem(Integer.toString(maxNumberOfPlayer));
139 settingComboBoxes[2].setSelectedItem(Integer.toString(maxNumberOfcon));
140 settingComboBoxes[3].setSelectedItem(Integer.toString(lengthOfALine));
141 settingComboBoxes[4].setSelectedItem(Integer.toString(numberOfColumns));
142 settingComboBoxes[5].setSelectedItem(Integer.toString(numberOfRows));
143 switch (playStyle) {
144 case GRAVITY:
145 settingComboBoxes[6].setSelectedIndex(0);
146 break;
147 case ANY_EMPTY_SPOT:
148 settingComboBoxes[6].setSelectedIndex(1);
149 break;
150 }
151 }
152
153 /***
154 * Invoked when an action occurs.
155 */
156 public void actionPerformed(ActionEvent actionEvent) {
157 if (actionEvent.getSource() == settingComboBoxes[0]) {
158 for (int i = 0; i < valueList.length; i++) {
159 if (settingComboBoxes[0].getSelectedIndex() == i) {
160 for (int j = 1; j < valueList[0].length; j++) {
161 settingComboBoxes[j].setSelectedItem(valueList[i][j]);
162 }
163 break;
164 }
165 }
166 } else {
167 if (actionEvent.getSource() == settingComboBoxes[1]) {
168 if (Integer.parseInt((String) settingComboBoxes[1].getSelectedItem())
169 > Integer.parseInt((String) settingComboBoxes[2].getSelectedItem())) {
170 settingComboBoxes[2].setSelectedItem(settingComboBoxes[1]
171 .getSelectedItem());
172 }
173 } else if (actionEvent.getSource() == settingComboBoxes[2]) {
174 if (Integer.parseInt((String) settingComboBoxes[1].getSelectedItem())
175 > Integer.parseInt((String) settingComboBoxes[2].getSelectedItem())) {
176 settingComboBoxes[1].setSelectedItem(settingComboBoxes[2]
177 .getSelectedItem());
178 }
179 }
180
181 for (int k = 1; k <= settingComboBoxes.length; k++) {
182 if (actionEvent.getSource() == settingComboBoxes[k]) {
183 for (int i = 0; i < valueList.length; i++) {
184 boolean predefinedValueBoolean = true;
185 for (int j = 1; j < valueList[0].length; j++) {
186 if (!settingComboBoxes[j].getSelectedItem().toString()
187 .equals(valueList[i][j])) {
188 predefinedValueBoolean = false;
189 break;
190 }
191 }
192 if (predefinedValueBoolean) {
193 settingComboBoxes[0].setSelectedIndex(i);
194 return;
195 }
196 }
197 settingComboBoxes[0].setSelectedIndex(valueList.length);
198 return;
199 }
200 }
201 }
202 }
203
204 public void setEnabled(boolean enabled) {
205 super.setEnabled(enabled);
206 for (int i = 0; i < settingComboBoxes.length; i++) {
207 settingComboBoxes[i].setEnabled(enabled);
208 }
209 }
210
211 public int getMaxNumberOfPlayer() {
212 return Integer.parseInt((String) settingComboBoxes[1].getSelectedItem());
213 }
214
215 public int getMaxNumberOfcon() {
216 return Integer.parseInt((String) settingComboBoxes[2].getSelectedItem());
217 }
218
219 public int getLengthOfALine() {
220 return Integer.parseInt((String) settingComboBoxes[3].getSelectedItem());
221 }
222
223 public int getNumberOfColumns() {
224 return Integer.parseInt((String) settingComboBoxes[4].getSelectedItem());
225 }
226
227 public int getNumberOfRows() {
228 return Integer.parseInt((String) settingComboBoxes[5].getSelectedItem());
229 }
230
231 public int getPlayStyle() {
232 int playStyle = GRAVITY;
233 int index = settingComboBoxes[6].getSelectedIndex();
234 switch (index) {
235 case 0:
236 playStyle = GRAVITY;
237 break;
238 case 1:
239 playStyle = ANY_EMPTY_SPOT;
240 break;
241 }
242 return playStyle;
243 }
244
245 }
246